﻿using AutoMapper;
using KingsSharp.Core;
using CRM.Admin.Contracts.Dto.Sys;
using CRM.Admin.Contracts.Model.Sys;
using KingsSharp.Common.Extensions;
using System.Linq;

namespace CRM.Admin.Services.Mappers
{
    public class SysMapper: IMapper
    {
        public void Register()
        {
            #region 菜单设置
            Mapper.CreateMap<MenuCatalogDto, MenuCatalog>();
            Mapper.CreateMap<MenuCatalog, MenuCatalogDto>()
                .ForMember(a => a.Pid, opt => opt.MapFrom(b => b.ParentMenuCatalog.Id))
                .ForMember(a => a.PName, opt => opt.MapFrom(b => b.ParentMenuCatalog.Name));

            Mapper.CreateMap<MenuNodeDto, MenuNode>()
                .ForMember(a => a.CatalogId, opt => opt.MapFrom(b => b.Pid))
                .ForMember(a => a.MenuURL, opt => opt.MapFrom(b => "~/" + b.Area + "/" + b.Controller + "/" + b.Action));



            Mapper.CreateMap<MenuNode, MenuNodeDto>()
                .ForMember(a => a.Pid, opt => opt.MapFrom(b => b.MenuCatalog.Id))
                .ForMember(a => a.PName, opt => opt.MapFrom(b => b.MenuCatalog.Name));
            #endregion

            #region 用户设置
            Mapper.CreateMap<UserInfoDto, UserInfo>();
            Mapper.CreateMap<UserInfo, UserInfoDto>()
                .ForMember(a => a.LastLoginDate, opt => opt.MapFrom(b => b.LastLoginDate != null ? b.LastLoginDate.ToSimple() : ""))
                .ForMember(a => a.RoleIds, opt => opt.MapFrom(b => b.UserRoles == null ? "" : string.Join(",", b.UserRoles.Select(a => a.RoleId).ToArray())));
            #endregion

            #region 数据字典
            Mapper.CreateMap<DictionaryDto, Dictionary>();
            Mapper.CreateMap<Dictionary, DictionaryDto>()
                .ForMember(a => a.Pid, opt => opt.MapFrom(b => b.Parent.Id))
                .ForMember(a => a.PName, opt => opt.MapFrom(b => b.Parent.Name));

            Mapper.CreateMap<DictionaryTypeDto, DictionaryType>();
            Mapper.CreateMap<DictionaryType, DictionaryTypeDto>()
                .ForMember(a => a.Pid, opt => opt.MapFrom(b => b.Parent.Id))
                .ForMember(a => a.PName, opt => opt.MapFrom(b => b.Parent.Name));

            #endregion

            #region 组织机构
            Mapper.CreateMap<OrganizationDto, Organization>();
            Mapper.CreateMap<Organization, OrganizationDto>()
                .ForMember(a => a.ParentOrganizationName, opt => opt.MapFrom(b => b.Parent != null ? b.Parent.Name : ""));
            #endregion

            #region 角色
            Mapper.CreateMap<RoleDto, Role>();
            Mapper.CreateMap<Role, RoleDto>()
                .ForMember(a => a.IsAdmin, opt => opt.MapFrom(b => b.IsAdmin ? "是" : "否"))
                .ForMember(a => a.IsLocked, opt => opt.MapFrom(b => b.IsLocked ? "是" : "否"))
                .ForMember(a => a.IsSystem, opt => opt.MapFrom(b => b.IsSystem ? "是" : "否"));
            #endregion

            #region 权限
            Mapper.CreateMap<AuthorizationDto, Authorization>();
            Mapper.CreateMap<Authorization, AuthorizationDto>();
            Mapper.CreateMap<UserRoleDto, UserRole>();
            Mapper.CreateMap<UserRole, UserRoleDto>();
            #endregion
        }
    }
}
